import { getType } from '@/utils/typeUtil'
export default {
  getJson(requestParamsList) {
    const map = Object.create(null)
    requestParamsList.forEach(param => {
      var obj = param.describe
      if (param.children !== undefined && param.children !== null && param.children !== '') {
        obj = this.getChildren(param.children)
      }
      if (param.array) {
        obj = [obj]
      }
      if (getType(param.dataType) !== 'file') {
        map[param.name] = obj
      }
    })
    return map
  },
  mapToObj(map) {
    const obj = Object.create(null)
    for (const [k, v] of map) {
      if (v instanceof Array) {
        v.forEach(m => {
          if (m instanceof Map) {
            this.mapToObj(m)
          }
        })
      } else {
        obj[k] = v
      }
    }
    return obj
  },
  getChildren(children) {
    const map = Object.create(null)
    if (children !== undefined && children !== null && children !== '') {
      children.forEach(param => {
        var obj = param.describe
        if (param.children !== undefined && param.children !== null && param.children !== '') {
          obj = this.getChildren(param.children)
        }
        if (param.array) {
          obj = [obj]
        }
        if (getType(param.dataType) !== 'file') {
          map[param.name] = obj
        }
      })
    }
    return map
  }
}
